package com.example.ponycui_home.svgaplayer;

import com.opensource.svgaplayer.*;
import com.opensource.svgaplayer.iml.ParseCompletion;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-22
 */
public class AnimationFromNetworkAbility extends Ability {
    SVGAImageView animationView = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#FF000000"));
        setUIContent(ResourceTable.Layout_ability_animation_from_assets);
        DirectionalLayout root = (DirectionalLayout)findComponentById(ResourceTable.Id_root);
        animationView = new SVGAImageView(this);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbPalette.GRAY);
        animationView.setImageElement(shapeElement);
        loadAnimation();
        root.addComponent(animationView, ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_PARENT);
    }

    private void loadAnimation() {
        try {
            SVGAParser svgaParser = SVGAParser.shareParser();
            svgaParser.setFrameSize(100,100);
            svgaParser.decodeFromURL(new URL("https://gitee.com/hanchao073/SVGA-Samples/raw/master/posche.svga"),
                    new ParseCompletion() {
                        @Override
                        public void onComplete( SVGAVideoEntity videoItem) {
                            animationView.setVideoItem(videoItem);
                            animationView.startAnimation();
                        }
                        @Override
                        public void onError() {

                        }
                    });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        animationView.stopAnimation(true);
    }
}
